<script setup lang="ts">
import type { ConsultOrderItem, Image } from "@/types/consult";
import { OrderType } from "@/enums";
import {uploadFileAPI} from '@/services/consult'

import { computed } from "vue";
import { ref } from "vue";
import { showLoadingToast, type UploaderFileListItem } from "vant";
const props = defineProps<{
  consult?: ConsultOrderItem
}>()

const disabled = computed(()=>{
  return props.consult?.status !== OrderType.ConsultChat
})

// import { uploadAPI } from '@/services/consult'
// import type { Picture } from '@/types/consult'
// import { showLoadingToast, type UploaderFileListItem } from 'vant'
// import { ref } from 'vue'

// 储存输入框数据
const msg = ref('')
const sendText = () => {
  emit('send-text', msg.value)
  msg.value = ''
}

// interface Props {
//   disabled: boolean
// }
// defineProps<Props>()

interface Emits {
  (eventName: 'send-text', msg: string): void
  (eventName: 'send-image', msg: Image): void
}
const emit = defineEmits<Emits>()

// const onSendText = () => {
//   emit('send-text', msg.value)
//   msg.value = ''
// }

const upload = async (item: UploaderFileListItem | UploaderFileListItem[]) => {
  if (Array.isArray(item)) return
  if (!item.file) return

  const toast = showLoadingToast('上传中')
  const { data } = await uploadFileAPI(item.file)
  console.log(data);
  
  toast.close()

  emit('send-image', data)
}
// }
</script>

<template>
  <div class="room-action">
    <van-field v-model="msg" @keypress.enter="sendText" :disabled="disabled" type="text" class="input" :border="false" placeholder="问医生" autocomplete="off" />
    <van-uploader :preview-image="false" :disabled="disabled" :after-read="upload" >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;

  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }

  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
